package com.zego.wawaji.ui.play;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.base.utils.InputUtils;
import com.zego.wawaji.third.zego.ZegoMgr;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.bean.user.UserDetail;
import com.zego.wawaji.wawaji_client.util.OnSoundClickListener;
import com.zego.zegoliveroom.callback.im.IZegoRoomMessageCallback;
import com.zego.zegoliveroom.constants.ZegoIM;

/**
 * 游戏输入框panel
 * Created by Su on 2017/11/25.
 */
public class PlayInputPanel extends BasePanel {
    private EditText chat_edit;
    private UserDetail userDetail;

    public PlayInputPanel(Context context) {
        super(context);
        setContentView(R.layout.play_input_panel);

        initView();
    }

    private void initView() {
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        chat_edit = (EditText) findViewById(R.id.chat_edit);
        findViewById(R.id.chat_send).setOnClickListener(listener);
        InputUtils.endCursor(chat_edit);
    }

    private void send() {
        final String text = chat_edit.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            PToast.showShort("请输入消息内容");
            return;
        }
        boolean success = sendMsg(text);
        if (!success)
            sendMsgFail();
        if (inputPanelListener != null)
            inputPanelListener.onInputbtnClick(null);
    }

    private boolean sendMsg(String text) {
        String content = userDetail.getNickname() + "：" + text;
        // 插入自己发送消息
        MsgMgr.getInstance().sendMsg(MsgType.MT_Msg_Receive, content);
        /**
         * 发送房间内广播消息。
         *
         * <p>发送成功后，房间内其他成员会通过 {@link IZegoIMCallback#onRecvRoomMessage(String, ZegoRoomMessage[])} 接收此消息</p>
         *
         * @param messageType       消息类型，   详见 {@link com.zego.zegoliveroom.constants.ZegoIM.MessageType}
         * @param messageCategory   消息分类，   详见 {@link com.zego.zegoliveroom.constants.ZegoIM.MessageCategory}
         * @param messagePriority   消息优先级， 详见 {@link com.zego.zegoliveroom.constants.ZegoIM.MessagePriority}
         * @param content           消息内容，长度 <= 1024 bytes 的可打印字符串
         * @param callback          实现 {@link IZegoRoomMessageCallback} 接口的对象实例，用于接收消息发送结果及 server 下发的 messageID
         * @return true:调用成功 等待 {@link IZegoRoomMessageCallback#onSendRoomMessage(int, String, long)} 返回, false:调用失败
         */
        return ZegoMgr.getInstance().getZegoLiveRoom().sendRoomMessage(ZegoIM.MessageType.Text, ZegoIM.MessageCategory.Chat, ZegoIM.MessagePriority.High,
                content, (i, s, l) -> {
                });
    }

    private void sendMsgFail() {
        getActivity().runOnUiThread(() -> PToast.showShort("发送失败,请稍后重试"));
    }

    public void refresh(int visible) {
        chat_edit.setText("");
        InputUtils.endCursor(chat_edit);
        if (visible == View.GONE) {
            InputUtils.forceClose((Activity) getContext());
            return;
        }
        InputUtils.forceOpen(getContext());
    }

    private OnSoundClickListener listener = new OnSoundClickListener() {
        @Override
        public void onSoundClick(View v) {
            switch (v.getId()) {
                case R.id.chat_send:
                    send();
                    break;
            }
        }
    };

    private InputPanelListener inputPanelListener;

    public void setInputPanelListener(InputPanelListener inputPanelListener) {
        this.inputPanelListener = inputPanelListener;
    }

    public interface InputPanelListener {
        void onInputbtnClick(View v);
    }
}
